#[macro_export]
macro_rules! srv_add {
    ($fn:ident, $form:ident, $create_fn:expr) => {
        pub async fn $fn(pool: web::Data<DbPool>, form: $form) -> Result<i32, ServiceError> {
            web::block(move || {
                let mut conn = pool.get()?;
                $create_fn(&mut conn, form.to())
            })
            .await?
        }
    };
}

#[macro_export]
macro_rules! srv_adds {
    ($fn:ident, $form:ident, $creates_fn:expr) => {
        pub async fn $fn(pool: web::Data<DbPool>, forms: Vec<$form>) -> Result<(), ServiceError> {
            web::block(move || {
                let mut conn = pool.get()?;
                let models = forms.iter().map(|form| form.to()).collect();
                $creates_fn(&mut conn, models)
            })
            .await?
        }
    };
}

#[macro_export]
macro_rules! srv_upd {
    ($fn:ident, $form:ident, $update_fn:expr) => {
        pub async fn $fn(pool: web::Data<DbPool>, form: $form) -> Result<usize, ServiceError> {
            web::block(move || {
                let mut conn = pool.get()?;
                $update_fn(&mut conn, form.id, form.to().to())
            })
            .await?
        }
    };
}

#[macro_export]
macro_rules! srv_dels {
    ($fn:ident, $form:ident, $deletes_fn:expr) => {
        pub async fn $fn(
            pool: web::Data<DbPool>,
            forms: Vec<$form>,
        ) -> Result<usize, ServiceError> {
            let ids = forms.into_iter().map(|form| form.id).collect();
            web::block(move || {
                let mut conn = pool.get()?;
                $deletes_fn(&mut conn, ids)
            })
            .await?
        }
    };
}

#[macro_export]
macro_rules! srv_del {
    ($fn:ident, $delete_fn:expr) => {
        pub async fn $fn(pool: web::Data<DbPool>, id: i32) -> Result<usize, ServiceError> {
            web::block(move || {
                let mut conn = pool.get()?;
                $delete_fn(&mut conn, id)
            })
            .await?
        }
    };
}
